﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Business.Compta;
using DAO.Compta;
using System.Windows.Controls.Primitives;

namespace Compta
{
    /// <summary>
    /// Interaction logic for CategoriesDefinition.xaml
    /// </summary>
    public partial class CategoriesDefinition : UserControl
    {

        public static ObservableCollection<Category> Categories
        {
            get
            {
                return new ObservableCollection<Category>(ComptaCategoriesDAO.getAllCategories());
            }
        }

        public CategoriesDefinition()
        {
            InitializeComponent();
        }

        private void ModifyRow(object sender, DevExpress.Wpf.Grid.GridRowValidationEventArgs e)
        {
            Category line = (Category)e.Row;
            if (line != null)
            {
                if (line.IdCategory == -1)
                {
                    try
                    {
                        ComptaCategoriesDAO.InserNewCategory(line);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("error while modifying or insert Category line ! (id = " + line.IdCategory + ")\n" + ex, "Error !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    try
                    {
                        ComptaCategoriesDAO.ModifyCategory(line);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("error while modifying or insert Category line ! (id = " + line.IdCategory + ")\n" + ex, "Error !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }


        private void deleteCategory(object sender, KeyEventArgs e)
        {

            if (e.Key == Key.Delete)
            {
                DevExpress.Wpf.Grid.TableView view = (DevExpress.Wpf.Grid.TableView)sender;
                if (view.ActiveEditor == null || !view.ActiveEditor.IsEditorActive)
                {
                    int listIndex = this.gridControl1.GetRowListIndex(view.FocusedRowHandle);
                    ObservableCollection<Category> list =
                        (ObservableCollection<Category>)this.gridControl1.DataSource;
                    if (listIndex >= 0)
                    {
                        Category line = list[listIndex];
                        try
                        {
                            int _return = ComptaCategoriesDAO.deleteCategory(line);

                            if (_return == 1)
                            {
                                list.RemoveAt(listIndex);
                            }
                            else
                            {
                                MessageBox.Show("La catégorie n'a pas été supprimée. Voir Mamour !", "ERROR !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("error while deleting category (id = " + line.IdCategory + ")\n" + ex.StackTrace, "ERROR !!!", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                }
            }
        }


    }

    public class IntToDoubleConverter : MarkupExtension,IMultiValueConverter {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            return this;
        }
        #region IValueConverter Members
        object IMultiValueConverter.Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Color c = new Color();
            return c;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        #endregion



    }

   
}
